const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/bilibili');

// once 事件回调函数只执行一次
mongoose.connection.once('connected', () => {
  // 设置集合中文档的属性以及属性的值
  let BookSchema = new mongoose.Schema({
      name: String,
      author: String,
      price: Number,
  });

  // 创建模型对象  对文档操作的封装对象
  let BookModal = mongoose.model('books', BookSchema);

  BookModal.create({
      name: '三国演义',
      author: '吴承恩',
      price: 30
  }).then((res, err) => {
      if (err) {
          console.log('创建失败');
      } else {
          console.log('创建成功');
          mongoose.disconnect();
      }
  })
});